package concurrency;

import java.util.Date;

/**
 * Created by duan lian on 2017-08-22.
 */
public class ThreadLocalVar {
	private ThreadLocalVar() {
	}

	private ThreadLocal<Date> date = ThreadLocal.withInitial(() -> {
//		System.out.println(Thread.currentThread().getName());
		return new Date();
	});

	Date getDate() {
		System.out.println(Thread.currentThread().getName());
		return date.get();
	}

	public static ThreadLocalVar instance = new ThreadLocalVar();

	public static void main(String[] args) throws InterruptedException {
		new Thread(() -> {
			System.out.println(instance.getDate().getTime());
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			System.out.println(instance.getDate().getTime());

		}, "thread-this").start();

		Thread.sleep(2032);

		System.out.println("-------------");

		new Thread(() -> {
			System.out.println(instance.getDate().getTime());
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			System.out.println(instance.getDate().getTime());

		}, "thread-that").start();
	}
}
